<?php
namespace app\index\controller;
use \org\Verify;
use \think\Session;
use app\index\model\Common;

class User extends HomeController
{
    public function index()
    {
      return view('index');
    }
    //登陆页面 /显示和登陆验证
    public function login()
    {
        if (IS_POST){
          $user=pt('user');
          $pwd=pt('pwd');
          $code=pt('code');
          //验证码 效验
          $ret = $this->check_verify(pt('code'));

          if (!$ret) return $this->error('验证码错误');

          $user = pt('user');
          if (!empty($user)) {
              $r = new Common();
              $user_id = $r->redis->get('user_tab:username:'.$user.':user_id');
              if (!$user_id) return $this->error('帐号不存在','/register');
              $pwd = $r->redis->get('user_tab:user_id:'.$user_id.':pwd');
              if ($pwd != md5(pt('pwd'))) {
                var_dump($pwd,md5(pt('pwd')));
                exit;
                  return $this->error('密码错误');
              } else {
                  $_SESSION['user_id'] = $user_id;
                  $_SESSION['username'] = $user;
                  return $this->success('登录成功','/');
              }
              exit;
          }
          exit;
        }
        $result = array();
        return view('login',$result);
    }


    //退出
    public function logout()
    {
        unset($_SESSION['user_id']);
        unset($_SESSION['username']);
        return $this->success('退出微博登录', '/');

    }
    //注册页面
    public function register()
    {
        if(IS_POST){
          $pwd=pt('pwd');
          $repwd=pt('repwd');
      if(pt('pwd') != pt('repwd')){
                return $this->error('两次密码输入不一致');
            }
            $r = new Common();
            $user = pt('user');
            $ret = $r->redis->exists('user_tab:username:'.$user.':user_id');
            if ($ret) return $this->error('帐号已存在');

            //开始写入
            $user_id = $r->redis->incr('global:user_id');
            $r->redis->set('user_tab:username:'.$user.':user_id',$user_id);
            $r->redis->set('user_tab:user_id:'.$user_id.':username',$user);
            $r->redis->set('user_tab:user_id:'.$user_id.':pwd',md5(pt('pwd')));
            $r->redis->set('user_tab:user_id:'.$user_id.':regip',$_SERVER['REMOTE_ADDR']);
            $r->redis->set('user_tab:user_id:'.$user_id.':regtime',date('Y-m-d H:i:s'));
            return $this->success('注册成功', 'login');
            exit;
        }
         $result = array(
         );
         return view('register',$result);
    }

     //注册返回
    public function regedit()
    {
        if(!$_POST){

            //处理数据 暂不过滤
           /* $user   = $_POST['user_name'];
            $pwd    = $_POST['post.user_pwd'];
            $repwd  = $_POST['post.user_repwd'];
            $code   = $_POST['post.vecode'];


            $data = array(
                'user' => $user,
                'pwd' => $pwd,
                'repwd' => $repwd,
                'code' => $code
            );*/
            $data = array(
                'user_name' => 'one',
                'user_pwd' => '123456',
                'user_repwd' => '123456',
                'vecode' => '1236'
            );
            $this->handle_regedit($data);


        }
        //请求方法错误
        return json(['status'=>0,'reason'=>'request method error']);
    }

    private function handle_regedit($data)
    {
        exit;
    }

    public function verify()
    {
      $config = array(
          'fontSize' => 20,    // 验证码字体大小
          'length'   => 4,     // 验证码位数
          'useNoise' => false, // 关闭验证码杂点
      );
      $Verify = new Verify($config);
      $Verify->entry();
    }

    public function check_verify($code, $id = ''){
      $verify = new Verify();
      return $verify->check($code, $id);
    }

}
